<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="128px">
      <el-form-item label="任务作业工单编号" prop="ticketNum">
        <el-input
          v-model="queryParams.ticketNum"
          placeholder="请输入任务作业工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务作业工单名称" prop="ticketName">
        <el-input
          v-model="queryParams.ticketName"
          placeholder="请输入任务作业工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="taskList">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="日期" align="center" prop="date" />
      <el-table-column label="任务作业工单编号" align="center" prop="taskPlan.ticketNum" />
      <el-table-column label="任务作业工单名称" align="center" prop="taskPlan.ticketName" />
      <el-table-column label="班次" align="center" prop="taskPlan.sailings" />
      <el-table-column label="班组" align="center" prop="taskPlan.deptName" />
      <el-table-column label="班组长" align="center" prop="taskPlan.morName" />
      <el-table-column label="验收人" align="center" prop="taskPlan.acceptorName" />
      <el-table-column label="任务状态" align="center" prop="taskPlan.status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="noted(scope.row)"
            v-hasPermi="['system:post:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="getback(scope.row)"
            v-hasPermi="['system:post:remove']"
          >撤回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日期" prop="startTime">
          <el-date-picker v-model="form.startTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss"
                          :style="{width: '100%'}" placeholder="请选择日期" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="班次" prop="sailings">
          <el-select v-model="form.sailings" placeholder="请选择班次" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in sailingpiton" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班组" prop="deptName">
          <el-cascader v-model="form.deptName" placeholder="请选择班组" clearable :style="{width: '100%'}"
          :options="deptNamepiton" >
          </el-cascader>
        </el-form-item>
        <el-form-item label="班组长" prop="morName">
          <el-input v-model="form.morName" :disabled='true' clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="验收人" prop="acceptorName">
          <el-select v-model="form.acceptorName" placeholder="请选择验收人" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in acceptorNamepion" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="钻孔编号" prop="borerId">
          <el-select v-model="form.borerId" placeholder="请选择钻孔编号" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in borerIdpion" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="钻孔类型" prop="borerType">
          <el-input v-model="form.borerType" :disabled='true' clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="钻机编号" prop="borerMachId">
          <el-select v-model="form.borerMachId" placeholder="请选择钻机编号" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in borerMachIdpion" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="钻机型号" prop="borerNum">
          <el-input v-model="form.borerNum" :disabled='true' clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="施工地点" prop="borerLoc">
          <el-select v-model="form.borerLoc" placeholder="请选择施工地点" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in borerLocpion" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划进尺" prop="poreDepth">
          <el-input v-model="form.poreDepth" placeholder="请输入计划进尺" />
        </el-form-item>
        <el-form-item label="备注" prop="drillExplain">
          <el-input v-model="form.taskExplain" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button type="primary" @click="submited">下 达</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="note" width="600px" append-to-body>
      <p>工单编号:{{info.ticketNum}}</p>
      <p>工单名称:{{info.ticketName}}</p>
      <hr>
      <p><strong>基础信息</strong></p>
      <p>班组:{{info.deptName}}  施工地点:{{info.borerLoc}}</p>
      <p>班组长:{{info.morName}}  计划进尺:{{info.poreDepth}}</p>
      <p>钻孔编号:{{info.borerId}}  钻孔类型:{{info.borerType}}</p>
      <p><strong>基础信息</strong></p>
      <p>任务起始时间:{{info.startTime}}</p>
      <p>任务结束时间:{{info.ddlTime}}</p>
      <p>验收人:{{info.acceptorName}}</p>
    </el-dialog>
  </div>
</template>


<script>
import {listTask, addTask, updateTask, getTask, delTask,getback,putoff, putnew} from "@/api/system/task";

export default {
  name: "Task",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      inWay:0,
      note:false,
      info: [],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ticketNum:undefined,
        ticketName:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [
          { required: true, message: "请选择日期", trigger: "blur" }
        ],
        sailings: [
          { required: true, message: "请选择班次", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "请选择班组", trigger: "blur" }
        ],
        morName:[],
        acceptorName:[
          {required: true, message: "请选择验收人", trigger: "blur"}
        ],
        borerId:[
          {required: true, message: "请选择钻孔编号", trigger: "blur"}
        ],
        borerType: [],
        borerMachId:[
          {required: true, message: "请选择钻机编号", trigger: "blur"}
        ],
        borerNum:[],
        borerLoc:[
          {required: true, message: "请选择施工地点", trigger: "blur"}
        ],
        poreDepth:[
          {required: true, message: "请输入计划进尺", trigger: "blur"}
        ],
        taskExplain:[]
      },
      sailingpiton:[{
        value: '零点班',
        label: '零点班'
      },{
        value: '八点班',
        label: '八点班'
      },{
        value: '四点班',
        label: '四点班'
      }],
      deptNamepiton:[{
        value: '抽采一队',
        label: '抽采一队',
        children:[{
          value: '生产一班',
          label: '生产一班',
        }]
      }],
      acceptorNamepion:[{
        value: '田鸡',
        label: '田鸡'
      }],
      borerIdpion:[{
        value: '2750',
        label: '2750'
      },{
        value: '2748',
        label: '2748'
      }],
      borerMachIdpion:[{
        value: '01',
        label: '01'
      },{
        value: '02',
        label: '02'
      }],
      borerLocpion:[{
        value: '40307回顺',
        label: '40307回顺'
      }],

    };
  },
  created() {
    this.getList();
  },
  methods: {
    noted(row){
      const num = row.taskPlan.ticketNum;
      getTask(num).then(response => {
        this.info = response.data;
        this.note=true;
        this.title="任务作业工单详情";
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.data.taskPlanAddDateDtos;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.inWay=0;
      this.form.morName="a";
      this.form.borerType="a";
      this.form.borerNum="a";
      this.open = true;
      this.title = "新建任务作业工单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.inWay=1;
      const num = row.taskPlan.ticketNum;
      getTask(num).then(response => {
        this.form = response.data;
        this.form.date=row.date;
        this.open = true;
        this.title = "修改任务作业工单";
      });
    },
    submited: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.inWay==1) {
            putoff(this.form.id).then(response => {
              this.$modal.msgSuccess("下达成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.parentDeptName="抽采一队";
            this.form.deptName ="生产一班";
            putnew(this.form).then(response => {
              this.$modal.msgSuccess("下达成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form);
          if (this.inWay==1) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form.deptName);
            this.form.parentDeptName="抽采一队";
            this.form.deptName ="生产一班";
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const num = row.taskPlan.ticketNum;
      this.$modal.confirm('是否确认删除编号为"' + num + '"的数据项？').then(function() {
        return delTask(num);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    getback(row){
      const num = row.taskPlan.id;
      const mem = row.taskPlan.ticketNum;
      this.$modal.confirm('是否确认撤回编号为"' + mem + '"的数据项？').then(function() {
        return getback(num);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤回成功");
      }).catch(() => {});
    },
  }
};
</script>
